package com.xpgk.mvc.index;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.xpgk.annotation.Service;
import com.xpgk.mvc.base.BaseService;
import com.xpgk.mvc.right.Right;
import com.xpgk.mvc.user.User;

@Service(name = IndexService.serviceName)
public class IndexService extends BaseService {

	@SuppressWarnings("unused")
	private static final Log log = Log.getLog(IndexService.class);

	public static final String serviceName = "indexService";

	/**
	 * 查询用户可操作的菜单
	 * 
	 * @param systemsIds
	 * @param user
	 * @param i18n
	 * @return
	 */
	public List<Right> menu(User user) {
		String stationIds = user.getStr(User.column_stationids);

		String oneSql = getSqlMy(Right.sqlId_one_menu);
		String twoSql = getSqlMy(Right.sqlId_two_menu);

		// 一级菜单
		List<Right> oneList = Right.dao.find(oneSql, stationIds);
		for (Right oneMenu : oneList) {
			// 二级菜单
			String pMenuIds = oneMenu.getPKValue();
			List<Right> twoList = Right.dao.find(twoSql, stationIds, pMenuIds);
			oneMenu.put("subList", twoList);
		}

		return oneList;
	}
	
	//当月业务统计图
	public List<Record> getRecord(String businessdate,String ids,String levelnum) {
		String sql1 = "SELECT c.name,NVL(b.ZCGD, 0) AS zcgd,NVL(b.ZYGD, 0) AS zygd,NVL(b.BGGD, 0) AS bggd,NVL(b.ZXGD, 0) AS zxgd,NVL(b.BHPGD, 0) AS bhpgd,NVL(b.BZJGD, 0) AS bzjgd FROM "+
				"SYS_ORGANIZATION c  "+
				"LEFT OUTER JOIN  "+
				"(SELECT a.soparentids as soparentids,SUM( dd.zcgd ) AS zcgd,SUM( dd.zygd ) AS zygd,SUM( dd.bggd ) AS bggd,SUM( dd.bhpgd ) AS bhpgd,SUM( dd.bzjgd ) AS bzjgd,SUM( dd.zxgd ) AS zxgd  FROM "+
				"(SELECT so.ids AS soids,so.PARENTIDS as soparentids FROM SYS_ORGANIZATION so  "+
				"WHERE so.PARENTIDS in (SELECT IDS FROM SYS_ORGANIZATION o WHERE o.PARENTIDS = ?)) a "+
				"LEFT OUTER JOIN DDC_DAILYSTASTIC dd ON DD.ORGANIDS = a.soids  "+
				"WHERE substr( dd.BUSSDATE, 0, 7 )= ?  "+
				"GROUP BY a.soparentids) b "+
				"ON c.IDS = b.soparentids "+
				"WHERE c.PARENTIDS = ? ORDER BY c.ORDERID";
		String sql2 = "SELECT c.name,NVL(b.ZCGD, 0) AS zcgd,NVL(b.ZYGD, 0) AS zygd,NVL(b.BGGD, 0) AS bggd,NVL(b.ZXGD, 0) AS zxgd,NVL(b.BHPGD, 0) AS bhpgd,NVL(b.BZJGD, 0) AS bzjgd FROM "+
				"SYS_ORGANIZATION c  "+
				"LEFT OUTER JOIN  "+
				"(SELECT a.soids as soids,SUM( dd.zcgd ) AS zcgd,SUM( dd.zygd ) AS zygd,SUM( dd.bggd ) AS bggd,SUM( dd.bhpgd ) AS bhpgd,SUM( dd.bzjgd ) AS bzjgd,SUM( dd.zxgd ) AS zxgd  FROM "+
				"(SELECT so.ids AS soids FROM SYS_ORGANIZATION so WHERE so.PARENTIDS = ?) a "+
				"LEFT OUTER JOIN DDC_DAILYSTASTIC dd ON DD.ORGANIDS = a.soids  "+
				"WHERE substr( dd.BUSSDATE, 0, 7 )= ?  "+
				"GROUP BY a.soids) b "+
				"ON c.IDS = b.soids "+
				"WHERE c.PARENTIDS = ? ORDER BY c.ORDERID";
		String sql3 = "SELECT a.yue AS yue,a.zcgd+a.zygd+a.bggd+a.bhpgd+a.bzjgd+a.zxgd AS totalyw FROM "+
				"(SELECT substr( dd.BUSSDATE, 6, 2 ) AS yue, "+
								"SUM( dd.zcgd ) AS zcgd, "+
								"SUM( dd.zygd ) AS zygd, "+
								"SUM( dd.bggd ) AS bggd, "+
								"SUM( dd.bhpgd ) AS bhpgd, "+
								"SUM( dd.bzjgd ) AS bzjgd, "+
								"SUM( dd.zxgd ) AS zxgd   "+
				"FROM DDC_DAILYSTASTIC dd  "+
				"WHERE dd.ORGANIDS = ? AND substr( dd.BUSSDATE, 0, 4 ) = ? "+
				"GROUP BY substr( dd.BUSSDATE, 6, 2 ) ORDER BY yue) a";
		List<Record> mm = null;
		if("0".equals(levelnum) || "1".equals(levelnum) || "2".equals(levelnum)){
			mm = Db.find(sql1,ids,businessdate,ids);
		}else if("3".equals(levelnum)){
			mm = Db.find(sql2,ids,businessdate,ids);
		}else if("4".equals(levelnum)){
			mm = Db.find(sql3,ids,businessdate);
		}
		return mm;
	}
	//当月业务统计图
	public List<Record> getRecord2(String ids) {
		String sql = "SELECT TYPE,COUNT(IDS) AS UNDONE FROM DDC_BUSINESS WHERE ORGANIDS = ? AND STATUS < 4 GROUP BY TYPE";
		List<Record> mm = Db.find(sql,ids);
		return mm;
	}
	
	/**
	 * 新增人数
	 * @param organ
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public Long getNewCustomerNum(String organIds, String registtime){
		//String sql = getSqlMy(IndexSQL.sqlId_newCustomerNum);
		Long num = 0l;
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("registtime", registtime);
		param.put("organIds", organIds);
		List<Record> lis = find(IndexSQL.sqlId_newCustomerNum, param);
		if(lis!=null&&lis.size()>0){
			 num =lis.get(0).getLong("num");
		}
		//Long num = Db.findFirst(sql).getLong("num");
		return num;
	}
	
	/**
	 * 减重销售金额
	 * @return
	 */
//	public Double getDelWeightSales(Organization organ, String startTime){
//		String sql = getSqlMy(IndexSQL.sqlId_delWeightSales);
//		Double prices = Db.findFirst(sql, startTime,  Code.lc_ids, organ.getStr(Organization.column_bh)).getDouble("prices");
//		return prices;
//	}
	
	/**
	 * 产品销售金额
	 * @param organ
	 * @param startTime
	 * @param endTime
	 * @return
	 */
//	public Double getProductSales(Organization organ, String startTime, String endTime){
//		String sql = getSqlMy(IndexSQL.sqlId_productSales);
//		Double prices = Db.findFirst(sql, startTime, endTime, Code.xflb_gm, Code.lc_ids, organ.getStr(Organization.column_bh)).getDouble("prices");
//		return prices;
//	}
	
	/**
	 * 减重销售明细
	 * @return
	 * @throws ParseException 
	 */
//	public List<Double> getDelWeightSalesDetail(Organization organ, List<String> days){
//		List<Double> result = new ArrayList<>();
//		for(String day : days){
//			String sql = getSqlMy(IndexSQL.sqlId_delWeightSalesByDay);
//			Double prices = Db.findFirst(sql, day, Code.lc_ids, organ.getStr(Organization.column_bh)).getDouble("prices");
//			if(null == prices){
//				result.add(0.0);
//			}else{
//				result.add(prices);
//			}
//		}
//		return result;
//	}
	
	/**
	 * 产品销售明细
	 * @param organ
	 * @param days
	 * @return
	 */
//	public List<Double> getProductSalesDetail(Organization organ, List<String> days){
//		List<Double> result = new ArrayList<>();
//		for(String day : days){
//			String sql = getSqlMy(IndexSQL.sqlId_productSalesByDay);
//			Double prices = Db.findFirst(sql, day, Code.lc_ids, Code.xflb_gm, organ.getStr(Organization.column_bh)).getDouble("prices");
//			if(null == prices){
//				result.add(0.0);
//			}else{
//				result.add(prices);
//			}
//		}
//		return result;
//	}
	
	
	

	public Double getDelWeight(String curOrganidS, String format) {
		Record  f = Db.findFirst("select SUM(gjWeight)   gjWeight  from  sb_customer_service_nurse  where   organids  = ?   and  YearMon  = ?",curOrganidS,format);
		if(f!=null&&f.getDouble("gjWeight")!=null){
			return f.getDouble("gjWeight");
		}
		return 0d;
	}
}
